package glossos.parsing;

import glossos.parsing.fsm.FiniteStateMachine;
import glossos.parsing.fsm.Parsers;
import glossos.tree.FrequencyTree;
import glossos.tree.snapshot.NodeRepository;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;


@ContextConfiguration(locations = "/test-context.xml")
@Transactional
@TransactionConfiguration(transactionManager = "txMgr")
public class ParsingTestCase extends AbstractTransactionalJUnit4SpringContextTests{

	WordBuffer buffer;
	Reader reader;
    Parser parser;
    Writer writerMock;
    FrequencyTree frequencyTree; 

    @Before
	public void setUp() {
        NodeRepository nodeRepository = (NodeRepository) applicationContext.getBean("nodeRepository");
        nodeRepository.removeAll();
        parser = (Parser) applicationContext.getBean("stateMachine");
        frequencyTree = (FrequencyTree)applicationContext.getBean("frequencyTree");
        writerMock = mock(Writer.class);
	}

    @Test    
    public void testParsing() throws IOException{
        frequencyTree.start();
        parser.parse();
        frequencyTree.logResults(writerMock);

        verify(writerMock).write("Most frequent sentence of size 1: in (3 occurrences)\n");
        verify(writerMock).write("Most frequent sentence of size 2: disposed to (2 occurrences)\n");
        verify(writerMock).write("Most frequent sentence of size 3: are sufficiently disposed (1 occurrences)\n");
        verify(writerMock).write("Most frequent sentence of size 4: are sufficiently disposed to (1 occurrences)\n");
        verify(writerMock).close();
    }
}
